package gui.frame;

import java.awt.Button;
import java.awt.Event;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;

public class FrameFileDialog extends Frame{

	TextField filename=new TextField();
	TextField directory=new TextField();
	Button openBtn=new Button("Open");
	Button saveBtn=new Button("Save");
	
	FrameFileDialog(){
		this.setTitle("File Dialog Test");
		Panel p=new Panel();
		p.setLayout(new FlowLayout());
		p.add(openBtn);
		p.add(saveBtn);
		this.add("South",p);
		this.directory.setEditable(false);
		this.filename.setEditable(false);
		p=new Panel();
		p.setLayout(new GridLayout(2,1));
		p.add(this.filename);
		p.add(this.directory);
		this.add("North",p);
	}
	
	public boolean handleEvent(Event evt){
		if(evt.id==Event.WINDOW_DESTROY){
			System.exit(0);
		}else
			return super.handleEvent(evt);
		return true;
	}
	
	public boolean action(Event evt,Object obj){
		if(evt.target.equals(openBtn)){
			FileDialog dialog=new FileDialog(this,"What file do you want to open ?");
			dialog.setFile("*.java");
			dialog.setDirectory(".");
			dialog.show();
			
			String openFile;
			if((openFile=dialog.getFile())!=null){
				this.filename.setText(openFile);
				this.directory.setText(dialog.getDirectory());
			}else{
				this.filename.setText("You pressed cancel");
				this.directory.setText("");
			}
		}
		else if(evt.target.equals(saveBtn)){
			FileDialog dialog=new FileDialog(this,"What file do you want to save?");
			dialog.setFile("*.java");
			dialog.setDirectory(".");
			dialog.show();
			String saveFile;
			
			if((saveFile=dialog.getFile())!=null){
				this.filename.setText(saveFile);
				this.directory.setText(dialog.getDirectory());
			}else
			{
				this.filename.setText("You pressed cancel");
				this.directory.setText("");
			}
		}
		else
			return super.action(evt, obj);
		return true;
	}
	
	public static void main(String[]args){
		Frame frame=new FrameFileDialog();
		frame.resize(350,200);
		frame.show();
	}
}
